#include<vector>
using namespace std;

class Solution {
public:
    int hIndex(vector<int>& citations) {
        int left = 0, right = citations.size()-1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (citations[mid] == (citations.size() - mid))
                return citations.size() - mid;
            if (citations[mid] > (citations.size() - mid))
                right = mid - 1;
            else
                left = mid + 1;
        }
        return citations.size() - 1 - right;
    }
};


//class Solution {
//public:
//    int hIndex(vector<int>& citations) {
//        int res = 0;
//        int index = 0;
//        while (index < citations.size()) {
//            if (citations[citations.size() - index+1] < index+1)
//                break;
//            ++index;
//        }
//        return index;
//    }
//};